package 力扣.二叉树相关问题.n叉树;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class n叉树的层序遍历 {
    class Node {
        public int val;
        public List<Node> children;
        public Node() {}
        public Node(int _val) {
            val = _val;
        }
        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> lists = new LinkedList<>();
        if (root==null)
            return lists;
        Queue<Node> q = new LinkedList();
        q.offer(root);
        while (!q.isEmpty()){
            int size = q.size();
            List<Integer> list = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                Node poll = q.poll();
                list.add(poll.val);
                for (Node child : poll.children) {
                    if (child!=null)
                        q.offer(child);
                }
            }
            lists.add(new LinkedList<>(list));
        }
        return lists;
    }

}
